﻿namespace star_questionnair.Common;

/**
 * 全局返回对象构造器
 */
public static class Results
{
    /**
     * 构造带返回数据的成功响应
     */
    public static Result<T> Success<T>(T data = default)
    {
        return new Result<T>
        {
            Code = Result<T>.SUCCESS_CODE,
            Data = data
        };
    }

    /**
     * 构造默认失败相应
     */
    public static Result<object> Failure(string errorCode = "500", string errorMessage = "Internal Server Error")
    {
        return new Result<object>
        {
            Code = errorCode,
            Message = errorMessage
        };
    }

    /**
     * 通过异常构建失败响应
     */
    public static Result<object> Failure(Exception exception)
    {
        return new Result<object>
        {
            Code = "500", // General server error code
            Message = exception?.Message ?? "An unexpected error occurred."
        };
    }
}